﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class SpeechTTS : System.Web.UI.Page
{

    public string gui = string.Empty;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        
        SpeechEngine.SpeechState state;
        if (string.IsNullOrEmpty(Request["g"]))
        {
            Guid guid = Guid.NewGuid();
            System.Speech.Synthesis.SpeechSynthesizer speech = new System.Speech.Synthesis.SpeechSynthesizer();
            state = new SpeechEngine.SpeechState()
            {
                Key = guid.ToString(),
                Speech = speech,
                InteractiveTime = DateTime.Now,
            };
            SpeechEngine.Instance.SpeechDictionary.TryAdd(guid.ToString(), state);
            gui = guid.ToString();
        }

        else
        {
            gui = Request["g"];
            SpeechEngine.Instance.SpeechDictionary.TryGetValue(gui, out state);
        }
        if (!string.IsNullOrEmpty(Request["s"]))
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                state.Speech.SetOutputToWaveStream(ms);
                state.Speech.Speak("开始播放声音");
                var codeBytes = ms.ToArray();
                Response.BinaryWrite(codeBytes);
            }

            //开始播放声音
            Response.End();
        }
    }


}
